#!/usr/bin/perl



#import modules

#map generator
use Games::RolePlay::MapGen;

#telnet module
use Net::Telnet;

#xml module
use XML::Simple;

#data dumper yay!
use Data::Dumper;


#main

#create a new map
createnewmap();

#load map as hash
my $mapobj = loadmapashash();

#connect to mud
#my $mudobj = connecttomud();

#create new zone
createnewzone($mapobj);









#subroutines

#createnewmap
sub createnewmap() {
	

	my $map = Games::RolePlay::MapGen->new({
	tile_size    => 10,
	cell_size    => "50x50", 
	num_rooms    => "2d4", 
	bounding_box => "50x50",
	max_room_size => "8x8",
	min_room_size => "3x3"
	}); 

	#my $map = Games::RolePlay::MapGen->import_xml("maptemplate.xml");

	$map->set_generator("Basic");             # This is actually the default generator,
	#$map->add_generator_plugin("BasicDoors"); # however, you must add the doors.
	$map->generate("map.txt");                # It'll generate a text map by default.
	$map->export("map/map.txt");
	$map->set_exporter( "XML" );              # But an xml map is probably more useful.
	$map->export("map/map.xml");
	$map->set_exporter( "PNG" );              # But a graphical map is probably more useful.
	$map->export("map/map.png");
	
}




#loadmapashash
sub loadmapashash() {
	
	my $xmlobj = XML::Simple->new(
									
									ForceArray => [ "closure" ]
									
								);
	
	my $hashref = $xmlobj->XMLin("map/map.xml");
	
	return $hashref;
	
}



#connecttomud
sub connecttomud() {
	
	#create object
	my $netobj = new Net::Telnet (	
									Host       => "localhost",
									Port       => 6666,
									Prompt     => '/\/.* >/',
									Timeout    => 5
									);
	
	#now connect it.
	if ( $netobj->open() )
	{
		print "Port Opened OK\n";
	}
	else
	{
		die();
	}
	
	#do the login
	$netobj->waitfor('/What name do you wish\?/');

	print "logging in\n";
	$netobj->print("Architect");

	$netobj->waitfor('/Password: /');

	$netobj->print("12345");

	$netobj->waitfor('/\/realms\/architect >/');

	@output = $netobj->cmd("l");
	
	print "logged in ok\n";
	
	return $netobj;
	
}




#createnewzone($mapobj);
sub createnewzone() {
	
	( $mapobj ) = @_;
	
	#for each row
	foreach my $row ( @{$mapobj->{'map'}->{'row'}} )
	{
		
		#now we are processing the row
		foreach my $cell ( @{ $row->{'tile'} } )
		{
			
			#skip blanks
			if ( $cell->{'type'} eq "wall" )
			{
				next;
			}
			
			
			#create the basic room
			my $room = "";
			$room .= "#include <lib.h>\n";
			$room .= "#include ROOMS_H\n";
			$room .= "\n";
			$room .= "inherit LIB_ROOM;\n";
			$room .= "\n";
			$room .= "static void create() {\n";
			$room .= "	room::create();\n";
			$room .= "	SetAmbientLight(30);\n";
			#	SetShort(\"an empty room\");";
			#	SetLong("An empty room with no exits leading anywhere.  It is "
			#			"completely barren with nothing to describe.");
			#}
			
			if ( $cell->{'type'} eq "corridor" )
			{
				$room .= "	SetShort(\"a corridor\");\n";
				$room .= "	SetLong(\"A corridor\");\n";
			}
			elsif ( $cell->{'type'} eq "room" )
			{
				$room .= "	SetShort(\"a room\");\n";
				$room .= "	SetLong(\"A room\");\n";
			}
			
			
			#make the exits
			#SetExits( ([ "north" : "/domains/Praxis/n_centre1",
            # "south" : "/domains/Praxis/s_centre1",
            # "east" : "/domains/Praxis/e_boc_la1",
            # "west" : "/domains/Praxis/w_boc_la1" ]) );
			
			my %exits;
			$exits{'north'} = "/domains/testdomain/room/area-" . $cell->{'xpos'} . "x" . ( $row->{'ypos'} - 1 );
			$exits{'east'} = "/domains/testdomain/room/area-" . ( $cell->{'xpos'} + 1 ) . "x" . $row->{'ypos'};
			$exits{'south'} = "/domains/testdomain/room/area-" . $cell->{'xpos'} . "x" . ( $row->{'ypos'} + 1 );
			$exits{'west'} = "/domains/testdomain/room/area-" . ( $cell->{'xpos'} - 1 ) . "x" . $row->{'ypos'};
			
			foreach my $mapwall ( @{ $cell->{'closure'} } )
			{
				
				delete($exits{$mapwall->{'dir'}});
				
			}
			
			$room .= "	SetExits( ([ \n";
			foreach my $newexit ( keys(%exits) )
			{
				$room .= "		\"" . $newexit . "\" : \"" . $exits{$newexit} . "\",\n";
			}
			$room =~ s/,\n$/\n/g;
			$room .= "	]) );\n";
			
			#and close off the room
			$room .= "}\n\n";
			$room .= "void init(){\n";
			$room .= "	::init();\n";
			$room .= "}\n";

			print $cell->{'xpos'} . "x" . $row->{'ypos'} . ":" . $cell->{'type'} . "\n";
			#print $room;
			
			my $fh;
			open($fh,">","area/area-" . $cell->{'xpos'} . "x" . $row->{'ypos'} . ".c");
			print $fh $room;
			close($fh);
		}
		
		print "\n";
		
	}
	
	
	
}


















